using Castle.MonoRail.Framework;
using MonorailTest.Core.Business;
using MonorailTest.Core.DTO;

namespace MonorailTest.Controllers
{
    [Layout("default"), Rescue("general")]
    public class PostController : SmartDispatcherController
    {
        private readonly IPostBusiness postBusiness;


        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PostController"/> class.
        /// </summary>
        /// <param name="postBusiness">The post business.</param>
        public PostController(IPostBusiness postBusiness)
        {
            this.postBusiness = postBusiness;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PostController"/> class.
        /// </summary>
        public PostController()
        {
            postBusiness = Global.WindsorContainer.Resolve<IPostBusiness>();
        }

        #endregion


        /// <summary>
        /// Adds this instance.
        /// </summary>
        public void Add()
        {
            RenderView("add");           
        }

        /// <summary>
        /// Saves the specified post.
        /// </summary>
        /// <param name="post">The post.</param>
        [AccessibleThrough(Verb.Post)]
        public void Save([DataBind("post")] NewPostDTO post)
        {
            var response = postBusiness.SavePost(post);
            if (response.PostSaved)
                PropertyBag["ResponseMessage"] = "The post 'post.title' was created";

            RenderView("postcreated");
        }
    }
}